package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 添加敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult addSensitive(WmSensitive wmSensitive) {
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (null!=sensitive){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmSensitive.setCreatedTime(new Date());
        this.save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 获取敏感词列表
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult getSensitiveList(SensitiveDto sensitiveDto) {
        if (sensitiveDto.getSize()==0||sensitiveDto.getPage()==0){
            sensitiveDto.setPage(1);
            sensitiveDto.setSize(10);
        }
        Page<WmSensitive> wmSensitivePage = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (null!=sensitiveDto.getName()){
            queryWrapper.like(WmSensitive::getSensitives,sensitiveDto.getName());
        }
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        page(wmSensitivePage,queryWrapper);
        ResponseResult result = new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(), (int) wmSensitivePage.getTotal());
        result.setData(wmSensitivePage.getRecords());
        return result;
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {

        return null;
    }
}
